import { createRouter, createWebHistory } from 'vue-router'

const router = createRouter({
  // vue2 mode:'hash/history'
  // vue3 history: create              createWebHistory====history
  history: createWebHistory(import.meta.env.BASE_URL),
  // import.meta.env.BASE_URL它就是环境变量，读取vue.config.ts内的base
  // history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/playground',
      component: () => import('@/views/PlayGround/index.vue'),
      meta: {
        title: '演示页'
      }
    },
    {
      path: '/login',
      component: () => import('@/views/login/index.vue'),
      meta: {
        title: '登录'
      }
    },
    {
      path: '/order/:id',
      component: () => import('@/views/order/order-detail.vue'),
      meta: { title: '药品订单详情' }
    },
    {
      path: '/login/callback',
      component: () => import('@/views/login/login-callback.vue'),
      meta: { title: 'QQ登录-绑定手机' }
    },
    {
      path: '/order/logistics/:id',
      component: () => import('@/views/order/order-logistics.vue'),
      meta: { title: '物流详情' }
    },
    {
      path: '/order/pay/result',
      component: () => import('@/views/order/order-pay-result.vue'),
      meta: { title: '药品支付结果' }
    },
    {
      path: '/order/pay',
      component: () => import('@/views/order/order-pay.vue'),
      meta: { title: '药品支付' }
    },
    {
      path: '/user/consult/:id',
      component: () => import('@/views/user/consult-detail.vue'),
      meta: { title: '问诊详情' }
    },
    {
      path: '/user/consult',
      component: () => import('@/views/user/consult-page.vue'),
      meta: { title: '问诊记录' }
    },
    {
      path: '/room',
      component: () => import('@/views/room/index.vue'),
      meta: { title: '问诊室' }
    },
    {
      path: '/consult/pay',
      component: () => import('@/views/consult/consult-pay.vue'),
      meta: { title: '问诊支付' }
    },
    {
      path: '/consult/fast',
      component: () => import('@/views/consult/consult-fast.vue'),
      meta: { title: '极速问诊' }
    },
    {
      path: '/consult/illness',
      component: () => import('@/views/consult/consult-illness.vue'),
      meta: { title: '病情描述' }
    },
    {
      path: '/consult/dep',
      component: () => import('@/views/consult/consult-dep.vue'),
      meta: { title: '极速问诊' }
    },
    {
      path: '/user/patient',
      component: () => import('@/views/user/patient-page.vue'),
      meta: { title: '家庭档案' }
    },
    {
      path: '/',
      component: () => import('@/views/layout/index.vue'),
      redirect: '/home',
      children: [
        {
          path: 'home',
          component: () => import('@/views/home/index.vue'),
          meta: {
            title: '首页'
          }
        },
        {
          path: 'article',
          component: () => import('@/views/article/index.vue'),
          meta: {
            title: '健康百科'
          }
        },
        {
          path: 'notify',
          component: () => import('@/views/notify/index.vue'),
          meta: {
            title: '消息中心'
          }
        },
        {
          path: 'user',
          component: () => import('@/views/user/index.vue'),
          meta: {
            title: '我的'
          }
        }
      ]
    }
    // {
    //   path: '/playtest',
    //   component: () => import('@/views/PlayGround/HmTest.vue')
    // }
  ]
})

// 导航守卫
/*
  vue2:next一定要用
  vue3:它推荐使用return 处理(不要在回调函数中定义next)  return 路由的地址===next(路由地址)  不写return 默认正常通过
       next在回调函数中定义了，就和vue2一样了

*/
import { useUserStore } from '@/stores'
const whitePage = ['/home', '/login', '/login/callback']

/*
 nprogress
 1:下载
   yarn add nprogress   yarn add @types/nprogress
 2:导入
    import Nprogress from 'nprogress'
    import 'nprogress/nprogress.css'
 3:
    Nprogress.start()
    Nprogress.done()

*/
import Nprogress from 'nprogress'
import 'nprogress/nprogress.css'

router.beforeEach(to => {
  Nprogress.start()
  // 没token 且不在白名单的页面，需要去登录页
  const store = useUserStore()
  if (!store.userInfo?.token && !whitePage.includes(to.path.toLowerCase())) {
    return '/login?redirect=' + encodeURIComponent(to.fullPath)
  }
})

router.afterEach(to => {
  //  document.querySelector('title')?.innerHTML
  document.title = '优医问诊-' + to.meta.title
  Nprogress.done()
})

export default router
